﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Utilities;
using Common.Utilities.TextMatch;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    public class FuzzyStringIndexer:IFieldIndexer
    {
        private Dictionary<string,int> _Words;
        /// <summary></summary>
        public Dictionary<string,int> Words
        {
            get
            {
                return this._Words;
            }
            set
            {
                this._Words = value;
            }
        }

        private List<string> _StopWords;
        /// <summary></summary>
        public List<string> StopWords
        {
            get
            {
                return this._StopWords;
            }
            set
            {
                this._StopWords = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public FuzzyStringIndexer()
        {
            this._Words=new Dictionary<string, int>();
            StopWordsUtil stopWordsUtil=new StopWordsUtil(StopWordsUtil.StopWordList.PubmedStopWordList);
            this._StopWords = stopWordsUtil.StopWords;
        }

        #region Implementation of IFieldIndexer

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fieldValue"></param>
        /// <param name="invertedIndexes"></param>
        public void IndexValue(int key, object fieldValue, ref Dictionary<int, List<int>> invertedIndexes)
        {
            if (fieldValue == null || fieldValue == DBNull.Value)
                return;
            string name = fieldValue.ToString().Trim();
            if (name.Length == 0)
                return;

            string[] words1 = StringUtil.Split(name.ToLower(), " ");
            List<string> wordList = new List<string>();
            foreach (string word in words1)
            {
                if (!string.IsNullOrEmpty(word) &&
                    !this._StopWords.Contains(word))
                {
                    wordList.Add(word);
                }
            }
            foreach (string word in wordList)
            {
                int idx = this._Words.Count;
                if (this._Words.ContainsKey(word))
                    idx = this._Words[word];
                else
                    this._Words.Add(word, idx);
                if (invertedIndexes.ContainsKey(idx))
                {
                    List<int> keys = invertedIndexes[idx];
                    if (!keys.Contains(key))
                        keys.Add(key);
                    invertedIndexes[idx] = keys;
                }
                else
                {
                    List<int> keys = new List<int>();
                    keys.Add(key);
                    invertedIndexes.Add(idx, keys);
                }
            }
        }

        #endregion
    }
}
